package za.org.jcicada.genss.sensor.impl;

import za.org.jcicada.genss.sensor.ISensorType;

/**
 * Float sensor type with legal range.
 * @author rcrida
 */
public class TypeFloat implements ISensorType {

	private static final long serialVersionUID = 1L;
	private float min;
    private float max;
    
    /**
     * Factory method used to access instance of TypeFloat
     * @return an instance of TypeFloat
     */
    static public TypeFloat getInstance(float min, float max) {
        return new TypeFloat(min, max);
    }
    
    /**
     * Factory method used to access instance of TypeFloat
     * @param args string array with min and max values
     * @return an instance of TypeFloat
     */
    static public TypeFloat getInstance(String[] args) {
        return getInstance(Float.parseFloat(args[0]), Float.parseFloat(args[1]));
    }
    
    /**
     * Private constructor forces use of getInstance().
     * @param min the minimum legal float value
     * @param max the maximum legal float value
     */
    private TypeFloat(float min, float max) {
        this.min = min;
        this.max = max;
    }
    
    public boolean isValid(Object value) {
        if (!(value instanceof Float)) {
            return false;
        }
        float val = ((Float)value).floatValue();
        return val >= min && val <= max;
    }
    
    @Override
    public String toString() {
    	return "Float";
    }

    @Override
    public boolean equals(Object aOther) {
        if (this == aOther) return true;
        if (!(aOther instanceof TypeFloat)) return false;
        TypeFloat other = (TypeFloat)aOther;
        return min == other.min && max == other.max;
    }
    
    @Override
    public int hashCode() {
        return Float.floatToIntBits(min) + Float.floatToIntBits(max);
    }

    public String[] toStringArray() {
        return new String[]{Float.toString(min), Float.toString(max)};
    }

    public Object valueOf(String string) {
        return Float.valueOf(string);
    }
    
    public double doubleValueOf(Object value) {
    	return ((Number) value).doubleValue();
    }
    
    public float getMin() {
    	return min;
    }
    
    public float getMax() {
    	return max;
    }

}
